package com.example.administrator.text;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import adapter.RecyclerWeWiFiAccount;
import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import entity.WeWiFiAccount;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.NetUtils;
import util.NetWorkstateUtil;
import util.RequestDataCallback;
import util.SpaceItemDecoration;
import util.StringUtil;
import util.Url;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.UploadDialog;

/**
 * 账号中心页        87311  625960  156 5900 2429
 */
public class WeWiFiAccountActivity extends BaseActivity{
    public static final String mAccount = "Account";
    private String mWWFAAccount,mId,mToken;
    public static final String mAction = "WIFICCOUNT-OK";
    private UploadDialog mUploadDialog;
    private IntentFilter mIntentFilter;
    private List<WeWiFiAccount.BindingInfoBean> mWeWiFiList;
    private RecyclerWeWiFiAccount mRecyclerWeWiWFAccount;

    @Bind(R.id.recycler_wewifi)         RecyclerView mRecyclerView;
    @Bind(R.id.frame_wewifi)            FrameLayout mFrameLayout;
    @Bind(R.id.activity_wewifi_tv)      TextView mTVLianwan;        //上网资源
    @Bind(R.id.textView4)               TextView mTV;
    @Bind(R.id.activity_wewifi_ll)      LinearLayout mLayoutLL;    //账号

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wewifi_account);
        ButterKnife.bind(this);
        init();
    }

    /**
     * 返回
     */
    @OnClick(R.id.wewifi_image_about) void wewifiRetuer(){
        finish();
    }

    /**
     * 绑定账号
     */
    @OnClick(R.id.linear_wewifi_account) void wewifiAccount(){
      if(mTV.getText().equals("解除绑定")){
           if(mWeWiFiList.size() > 0){
               String account = mWeWiFiList.get(0).getAccount();
               String bindingid = mWeWiFiList.get(0).getBindingid();
               JumpActivityUtils.getInstance().jumpRelieveDing(this,account,bindingid);
           }
      }else {
          JumpActivityUtils.getInstance().jumpWeBinDingPhone(this,"","ACCOUNT");
      }
    }

    /**
     * 初始化
     */
    private void init() {

        mWWFAAccount = getIntent().getStringExtra("account");
        Log.e("mWBDAABanding",""+mWWFAAccount);
        HomeUtil.getHemeUtilNew().getStatusBar(this,mFrameLayout);
        LinearLayoutManager linearLayoutManagaer = new LinearLayoutManager(this);
        linearLayoutManagaer.setOrientation(LinearLayout.VERTICAL);

        mId = (String) SharedPreferencesUtils.getParam(this,HomeUtil.mUseridDB,"");
        mToken = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mTokenDB, "");
        mRecyclerView.setLayoutManager(linearLayoutManagaer);
        mRecyclerView.setHasFixedSize(true);//如果确定每个item的内容不会改变RecyclerView的大小，设置这个选项可以提高性能
        getRecyclerView();
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(mAction);
        registerReceiver(broadcastReceiver,mIntentFilter);
    }

    /**
     * 获取RecyclerView需要的数据
     */
    private void getRecyclerView(){
        String url = "";
        if(mUploadDialog == null){
            mUploadDialog = new UploadDialog(this,"加载中");
            mUploadDialog.show();
        }else {
            mUploadDialog.show();
        }

        mMap = new HashMap<>();
        mMap.put("userid",mId);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngBinding_bindingList,stringJson,mToken,mId,time);
        LogUtil.eE("Gson",MyApplication.getApp().getFWQString());
        final Map map = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId,Url.mStirngBinding_bindingList,stirngSign,time);
        if(NetUtils.getInstance(this).getNetWorkTeyt() == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = MyApplication.getApp().getFWQString();
        }

        getLogger().info("getRecyclerView()--->账号列表--url"+url+"stirngSign"+stirngSign);
        GreenTreeNetworkUtil.getInstance().doPost(this,url,1,map, new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                getLogger().info("getRecyclerView()--->onSuccess（）--String"+response);
                Gson gson = new Gson();
                mUploadDialog.dismiss();
                WeWiFiAccount wewifi = gson.fromJson(response, WeWiFiAccount.class);
                if(wewifi.getCode() ==1 ){
                    mWeWiFiList = wewifi.getBindingInfo();
                    if(mWeWiFiList.size() == 0){
                        mTV.setText("绑定账号");
                        mRecyclerView.setVisibility(View.GONE);//因为再次加载时item 会显示不全 并且RecyclerView的高度全屏，下面的控件还不显示 所以只要用死办法，下次再改进
                        return;
                    }else {
                        mTV.setText("解除绑定");
                    }
                    mRecyclerView.setVisibility(View.VISIBLE);
                    mRecyclerWeWiWFAccount = new RecyclerWeWiFiAccount(WeWiFiAccountActivity.this,1,mWeWiFiList,null,null);
                    mRecyclerView.setAdapter(mRecyclerWeWiWFAccount);
                    mRecyclerView.addItemDecoration(new SpaceItemDecoration(WeWiFiAccountActivity.this, LinearLayoutManager.VERTICAL,15,R.color.wewifi_ba));
                    mRecyclerWeWiWFAccount.setOnItemClickListener(new RecyclerWeWiFiAccount.OnRecyclerViewItemClickListener() {
                        @Override public void onItemClick(View view, int position) {
                            if(mWWFAAccount.equals(mAccount)){

                            }else {
                            String account = mWeWiFiList.get(position).getAccount();
                            String bindingid = mWeWiFiList.get(position).getBindingid();
                            JumpActivityUtils.getInstance().jumpRelieveDing(WeWiFiAccountActivity.this,account,bindingid);
                            }
                        }});
                    mRecyclerWeWiWFAccount.notifyDataSetChanged();
                   // AltairIMLogUtil.eE("WIFL",mRecyclerView.gett);

                }else if(wewifi.getCode() ==-1){Toast.makeText(WeWiFiAccountActivity.this, getString(R.string.FWQ), Toast.LENGTH_SHORT).show();
                }else if(wewifi.getCode() ==-2){
                    CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(WeWiFiAccountActivity.this);
                    crowdRetunDialog.show();
                }
              }

            @Override public void onFailure(Throwable error) {
                mUploadDialog.dismiss();
                StringUtil.getStringUtilNew().getExceptionCode(WeWiFiAccountActivity.this, error.toString());}
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.eE("","onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.eE("","onResume");
        if(Url.getUrlNew().getNETWORKING_ATATE() == 1){
            NetWorkstateUtil.getInstance().getBrokenNetwork(this, new HomeUtil.OnHomeUtilClickListener() {
                @Override
                public void onItemClick(String type) {

                }
            },1);
        }

    }


    @Override
    protected void onStop() {
        super.onStop();
        LogUtil.eE("","onStop");
    }

    @Override protected void onDestroy() {
        super.onDestroy();
        if(mUploadDialog !=null){
            mUploadDialog.dismiss();
        }
        if(mWeWiFiList !=null){mWeWiFiList.clear();}

        if(broadcastReceiver !=null){unregisterReceiver(broadcastReceiver);}
    }

    /**
     *创建一个动态广播
     */
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override public void onReceive(Context context, Intent intent) {
            LogUtil.eE("","broadcastReceiver。。。。。。进来");
            getRecyclerView();
        }
    };

}
